package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @author ycf
 */
@Controller
public class MyController {

    @Autowired
    private UserService userService;

    /**
     * 首页
     * @return
     */
    @RequestMapping({"/index", "/index.html", "/"})
    public String index() {
        return "index";
    }


    /**
     * 返回所有的用户并显示在页面上
     * @param model
     * @return
     */
    @RequestMapping("/userList")
    public String userList(Model model, String username, String password, HttpSession session) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            model.addAttribute("loginInfo", "账号密码不能为空");
            return "index";
        } else if (username.equals("admin") && password.equals("123456")) {
            session.setAttribute("username", "admin");
            List<User> userList = userService.getUserList();
            model.addAttribute("list", userList);
            return "userList";
        } else {
            model.addAttribute("loginInfo", "账号密码错误");
            return "index";
        }
    }

    /**
     * 返回查询用户列表页面
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("/user/searchUser")
    public String searchUser(String username, Model model) {
        List<User> userList = userService.getUserLikeUsername(username);
        model.addAttribute("list", userList);
        System.out.println("username = " + username);
        System.out.println("userList = " + userList);
        return "userList";
    }


}
